#ifndef __THREADPOOL_H__
#define __THREADPOOL_H__

#include <unistd.h>
#include <pthread.h>
#include <sys/types.h>  
 
typedef void* (*CALLBACK)(void* arg); 

struct task
{
  CALLBACK callback_function;   //线程的回调函数
  void* arg;                                                    //线程回调函数的参数
  struct task* next;
};

struct threadpool
{
  int thread_num;               //线程池中开启线程的个数
  int quene_max_num;      //队列中任务最大个数
  
  struct task* head;            //指向任务队列的头指针
  struct task* tail;           //指向任务队列的尾指针
  
  pthread_t * threadids;     //线程池中所有线程的ID
  pthread_mutex_t mutex;  //互斥锁
  
  pthread_cond_t  quene_empty;  //队列是否为空
  pthread_cond_t  quene_not_empty; //队列是否不为空
  pthread_cond_t  quene_not_full;               //队列是否为不满
  
  int quene_cur_num;                 //队列当前任务个数
  int quene_close;                   //队列状态是否关闭
  int pool_close;                    //线程池状态是否关闭
};
void* thread_function(void* arg);
struct threadpool* thread_pool_init(int thread_num,int quene_max_num);
int threadpool_add_task(struct threadpool* pool,CALLBACK callback_function,void* arg);
int threadpool_destroy(struct threadpool* pool);
#endif


